<template>
    <el-card>
        <template #header>
            <h3>添加店铺</h3>
        </template>
        <el-form :model="shopData" label-width="120">
            <el-form-item label="店铺名称">
                <el-input placeholder="店铺名称" v-model="shopData.shopName"></el-input>
            </el-form-item>
            <el-form-item label="店铺地址">
                <el-input placeholder="店铺地址" v-model="shopData.shopAddress"></el-input>
            </el-form-item>
            <el-form-item label="店铺图片">
                <el-upload class="avatar-uploader" action="http://127.0.0.1:8080/javaweb_ele/upload"
                    :show-file-list="false" :on-success="handleAvatarSuccess">
                    <img v-if="shopData.shopImg" :src="shopData.shopImg" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon">
                        <Plus />
                    </el-icon>
                </el-upload>
            </el-form-item>
            <el-form-item label="店铺类型">
                <el-select v-model="shopData.shopCategoryId">
                    <el-option v-for="item in shopCategoryList" :label="item.categoryName"
                        :value="item.shopCategoryId"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="营业开始时间">
                <el-time-select v-model="shopData.startTime" start="00:00" step="00:30" end="23:59"
                    placeholder="营业开始时间" />
            </el-form-item>
            <el-form-item label="营业结束时间">
                <el-time-select v-model="shopData.endTime" start="00:00" step="00:30" end="23:59"
                    placeholder="营业结束时间" />
            </el-form-item>
            <el-form-item label="起送费">
                <el-input placeholder="起送费" v-model="shopData.startPrice"></el-input>
            </el-form-item>
            <el-form-item label="配送费">
                <el-input placeholder="配送费" v-model="shopData.deliveryPrice"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" placeholder="备注" v-model="shopData.remarks"></el-input>
            </el-form-item>
            <el-form-item label="店铺介绍">
                <el-input type="textarea" placeholder="店铺介绍" v-model="shopData.shopExplain"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="add">提交</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>
import { shopCategoryListAPI, shopAddAPI } from "@/api/shop.js";
export default {
    data() {
        return {
            shopData: {},
            shopCategoryList: [],
            businessInfo: {},
        };
    },
    methods: {
        // 图片上传之后之后，触发的函数
        handleAvatarSuccess(res) {
            console.log(res);
            this.shopData.shopImg = res;
        },
        // 点击提交按钮
        add() {
            // 需要从本地存储中，获取到商家信息，从其中拿到商家id
            this.shopData.businessId = this.businessInfo.id;
            // 调用axios封装函数，将数据提交给后端
            shopAddAPI(this.shopData).then(resp => {
                if (resp.data == 1) {
                    ElNotification({
                        type: "success",
                        message: "添加成功",
                    });
                    this.$router.push("/shopList");
                }
            })

        },
        // 加载店铺分类的数据
        loadShopCategory() { },
    },
    created() {
        // 从本地存储中获取商家登录的信息
        let info = localStorage.getItem("businessInfo");
        if (!info) {
            // 从本地存储中没有获取到商家的信息，说明商家没有登录，踢回登录页面上
            this.$router.push("/businessLogin");
            return;
        }
        this.businessInfo = JSON.parse(info);

        this.loadShopCategory(
            shopCategoryListAPI().then((resp) => {
                this.shopCategoryList = resp.data;
            })
        );
    },
};


</script>

<style scoped>
.el-card {
    width: 70%;
}

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>